package com.wanyang.service;

import com.github.pagehelper.PageHelper;
import com.wanyang.bo.MemberBO;
import com.wanyang.dao.CommonDao;
import com.wanyang.dao.GoldShopDao;
import com.wanyang.dos.GoldGoodsDO;
import com.wanyang.dos.GoldGoodsDOExample;
import com.wanyang.dos.GoldOrderDO;
import com.wanyang.dos.GoldRecordDO;
import com.wanyang.dto.PageDTO;
import com.wanyang.exception.ApiException;
import com.wanyang.mapper.GoldGoodsDOMapper;
import com.wanyang.mapper.GoldOrderDOMapper;
import com.wanyang.mapper.GoldRecordDOMapper;
import com.wanyang.utils.DateUtil;
import com.wanyang.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class GoldShopServiceImpl implements GoldShopService {
    @Resource
    private GoldShopDao goldShopDao;

    @Resource
    private GoldGoodsDOMapper goldGoodsDOMapper;

    @Autowired
    private MemberService memberService;

    @Resource
    private CommonDao commonDao;

    @Resource
    private GoldRecordDOMapper goldRecordDOMapper;

    @Resource
    private GoldOrderDOMapper goldOrderDOMapper;

    @Override
    public List<GoodsVO> selectGoodsList(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO);
        return goldShopDao.selectGoodsList();
    }

    @Override
    public GoldGoodsDO selectGoodsDO(Integer id) {
        GoldGoodsDOExample example = new GoldGoodsDOExample();
        example.createCriteria()
                .andIdEqualTo(id)
                .andIsDeleteEqualTo(false);
        return goldGoodsDOMapper.selectOneByExample(example);
    }

    @Override
    @Transactional
    public void exchange(Integer goodsId, Integer quantity) {
        // 检查
        GoldGoodsDO goldGoodsDO = goldGoodsDOMapper.selectByPrimaryKey(goodsId);
        if (goldGoodsDO == null || goldGoodsDO.getIsDelete()) {
            throw new ApiException("兑换商品不存在");
        }

        if (goldGoodsDO.getQuantity() < quantity) {
            throw new ApiException("库存不足");
        }

        MemberBO memberBO = UserContext.get();



        if (memberBO.getGold().intValue() < (goldGoodsDO.getNeedGold().intValue() * quantity) ) {
            throw new ApiException("金币不足");
        }

        // 扣库存
        boolean result = operateGoodsQuantity(goodsId, quantity);

        if (!result) {
            throw new ApiException("库存不足");
        }

        // 扣减用户积分
        BigDecimal reduceGold = goldGoodsDO.getNeedGold().multiply(new BigDecimal(quantity));
        result = memberService.operateGold(memberBO.getId(), reduceGold);

        if (!result) {
            throw new ApiException("金币不足");
        }

        // 金币扣减流水
        GoldRecordDO recordDO = new GoldRecordDO();
        recordDO.setGold(BigDecimal.ZERO.subtract(reduceGold));
        recordDO.setMemberId(memberBO.getId());
        recordDO.setCreateTime(DateUtil.currentUnixTime());
        recordDO.setRemark("金币商城兑换");
        goldRecordDOMapper.insertSelective(recordDO);
        Integer recordId = commonDao.mysqlLastId();

        // 生成兑换订单
        GoldOrderDO orderDO = new GoldOrderDO();
        orderDO.setMemberId(memberBO.getId());
        orderDO.setGoodsId(goodsId);
        orderDO.setIsSettled(false);
        orderDO.setCreateTime(DateUtil.currentUnixTime());
        orderDO.setGoldRecordId(recordId);
        goldOrderDOMapper.insertSelective(orderDO);


    }

    @Override
    public boolean operateGoodsQuantity(Integer goodsId, Integer quantity) {
        return goldShopDao.operateGoodsQuantity(goodsId, -quantity) > 0;
    }
}
